﻿using MediatR;
using W.Purchase.Domain.PageDTO;
using W.Purchase.Domain;
using W.Purchase.Read.API.AppLication.Command.ReceivableCommand;
using W.Purchase.Repository.Interface;
using W.Common.Enum;
using System.Linq;
using W.Purchase.Domain.DTO;

namespace W.Purchase.Read.API.AppLication.CommandHandler.ReceivableCommandHandler
{
    public class ReceivableShowCommandHandler : IRequestHandler<ReceivableShowCommand, Pages<ReceivableShowDto>>
    {
        private readonly IReceivableRepository _ireceivableRepository;
        public ReceivableShowCommandHandler(IReceivableRepository ireceivableRepository)
        {
            _ireceivableRepository = ireceivableRepository;
        }

        public async Task<Pages<ReceivableShowDto>> Handle(ReceivableShowCommand request, CancellationToken cancellationToken)
        {
            Pages<ReceivableShowDto> page = new Pages<ReceivableShowDto>();
            var list = from a in await _ireceivableRepository.GetAllAsaync()
                       where a.IsDelete==false
                       select new ReceivableShowDto
                       {
                           ReceivableId=a.ReceivableId,
                           ReceivableSerialNumber=a.ReceivableSerialNumber,
                           ReceivableName=a.ReceivableName,
                           FinanceTypeName=a.FinanceTypeName,
                           EndDate=a.EndDate,
                           Payer=a.Payer,
                           Payee=a.Payee,
                           ReceivableMoney=a.ReceivableMoney,
                           PracticalMoey=a.PracticalMoey,
                           ResidueMoney=a.ResidueMoney,
                           Status=a.Status,
                           StatusText=a.Status.ToString(),
                           PaymentNum=a.PaymentNum,
                           Receivableremark=a.Receivableremark,
                           office_id=a.office_id,
                           user_id=a.user_id,
                           CreateTime=a.CreateTime,
                           IsDelete=a.IsDelete,
                       };
            if (!string.IsNullOrEmpty(request.ReceivableSerialNumber))
            {
                list = list.Where(c => c.ReceivableSerialNumber == request.ReceivableSerialNumber).ToList();
            }
            if (!string.IsNullOrEmpty(request.ReceivableName))
            {
                list = list.Where(c => c.ReceivableName.Contains(request.ReceivableName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.FinanceTypeName))
            {
                list = list.Where(c => c.FinanceTypeName == request.FinanceTypeName).ToList();
            }
            if (!string.IsNullOrEmpty(request.Payer))
            {
                list = list.Where(c => c.Payer.Contains(request.Payer)).ToList();
            }
            if (request.StartTime != null)
            {
                list = list.Where(c => c.CreateTime >= request.StartTime).ToList();
            }
            if (request.EndTime != null)
            {
                list = list.Where(c => c.CreateTime <= request.EndTime).ToList();
            }
            if (request.Status > 0)
            {
                list = list.Where(c => c.Status == (ReceivableStatus)request.Status).ToList();
            }
            page.TotalCount = list.Count();
            page.TotalPage = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
            page.List = list.OrderByDescending(c=>c.ReceivableId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return page;
        }
    }
}
